from PySide6.QtGui import QMouseEvent
from PySide6.QtWidgets import QWidget,QApplication
from PySide6.QtGui import QBitmap,QMouseEvent,QCursor,QPainter,QPixmap,QPaintEvent
from PySide6.QtCore import Qt,QTimer

import sys

class ShapWidget(QWidget):
    def __init__(self,parant=None):
        super(ShapWidget,self).__init__()
        # self.setWindowTitle("不规则窗体")
        self.i=1
        self.updatePix()
        self.timer=QTimer()
        self.timer.setInterval(1000)  #1000毫秒
        self.timer.timeout.connect(self.timeChange)
        self.timer.start()
        
    def updatePix(self):
        if self.i==5:
            self.i=1
        self.mypic={1:"./images/left.png",2:"./images/up.png",3:"./images/right.png",4:"./images/down.png"}
        self.pix=QPixmap(self.mypic[self.i],"0",Qt.AvoidDither|Qt.ThresholdDither|Qt.ThresholdAlphaDither)
        self.resize(self.pix.size())
        self.setMask(self.pix.mask())
        self.dragPosition=None
        self.m_drag=False
        self.update()
        
    def mousePressEvent(self,event:QMouseEvent):
        if event.button() ==Qt.LeftButton:
            self.m_drag=True
            self.m_DragPosition=event.globalPosition().toPoint()-self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))
        if event.button()==Qt.RightButton:
            self.close()
            
    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        if Qt.LeftButton and self.m_drag:
            #当左键移动窗体修改偏移值
            self.move(event.globalPosition().toPoint()-self.m_DragPosition)
            event.accept()
    def mouseReleaseEvent(self, event: QMouseEvent) -> None:
        self.m_drag=False
        self.setCursor(QCursor(Qt.ArrowCursor))
        
    #一般paintEvent在窗体首次绘制加载，要重新加载paintevent需要重新加载窗口使用
    #self.update() or self.repaint()
    
    def paintEvent(self,event: QPaintEvent):
        painter=QPainter(self)
        painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),self.pix)
        
    def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
        if event.button()==1:
            self.i+=1
            self.updatePix()
    def timeChange(self):
        self.i+=1
        self.updatePix()
        
        
        
if __name__=="__main__":
    app=QApplication(sys.argv)
    win=ShapWidget()
    win.show()
    app.exec()
            